﻿using System;
using System.Collections.Generic;
using System.Linq;

using System.IO;
using System.Net;
using System.Text; 

namespace ServiceHost
{
    public class PushNotificationHelper
    {
        const string appKey = "Wfbl0ed-Qby1cn0BBeYnyw";
        private const string appSecret = "3o1f9LY-RRuCUb6TZ0gSDA";

        public static bool SendPushNotification(List<string> tokens)
        {
            //TODO: send tokens to Urban Airship
            if (tokens.Count == 0)
            {
                return false;
            }

            // Create a request using a URL that can receive a post.  
            WebRequest request = WebRequest.Create("https://go.urbanairship.com/api/push/"); 
            request.Credentials = new NetworkCredential(appKey, appSecret); 
            // Set the Method property of the request to POST. 
            request.Method = "POST"; 
            // Create POST data and convert it to a byte array. 
            //WRITE JSON DATA TO VARIABLE D 

            var tokenString = string.Empty;
            foreach (var token in tokens)
            {
                tokenString += "\", \"" + token;
            }
            tokenString = tokenString.Substring(4, tokenString.Length - 4);

            string postData = "{\"aps\": {\"badge\": 1, \"alert\": \"Fire Alert!\"}, \"device_tokens\": [\"" + tokenString + "\"]}";//6334c016fc643baa340eca25bc661d15055a07b475e9a6108f3f644b15dd05ab\", \"6334c016fc643baa340eca25bc661d15055a07b475e9a6108f3f644b15dd05ac\"]}"; 
            byte[] byteArray = Encoding.UTF8.GetBytes(postData); 
            // Set the ContentType property of the WebRequest. 
            request.ContentType = "application/json"; 
            // Set the ContentLength property of the WebRequest. 
            request.ContentLength = byteArray.Length; 
            // Get the request stream. 
            Stream dataStream = request.GetRequestStream(); 
            // Write the data to the request stream. 
            dataStream.Write(byteArray, 0, byteArray.Length); 
            // Close the Stream object. 
            dataStream.Close(); 
            // Get the response. 
            WebResponse response = request.GetResponse(); 
            // Display the status. 
            //            Console.WriteLine(((HttpWebResponse)response).StatusDescription); 
            // Get the stream containing content returned by the server. 
            dataStream = response.GetResponseStream(); 
            // Open the stream using a StreamReader for easy access. 
            StreamReader reader = new StreamReader(dataStream); 
            // Read the content. 
            string responseFromServer = reader.ReadToEnd(); 
            // Display the content. 
            Console.WriteLine(responseFromServer); 
            // Clean up the streams. 
            reader.Close(); 
            dataStream.Close(); 
            response.Close();

            return true;
        } 
    } 
} 
